package com.wangshuqi.cookies;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

public class GetCookiesServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取客户端所有cookie
        Cookie[] cookies = req.getCookies();
        StringBuilder sb = new StringBuilder(cookies.length);
        for (Cookie cook : cookies) {
            System.out.println(cook.getName() + ":" + cook.getValue() + " | ");
            sb.append(cook.getName() + ":" + cook.getValue() + " |\n");
        }
        resp.setCharacterEncoding("gbk");
        resp.getWriter().print(sb.toString());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
